package com.mio.web.controller.message;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.durcframework.core.GridResult;
import org.durcframework.core.MessageResult;
import org.durcframework.core.UserContext;
import org.durcframework.core.controller.CrudController;
import org.durcframework.core.expression.ExpressionQuery;

import com.mio.web.common.MioConstants;
import com.mio.web.common.MioInfoTips;
import com.mio.web.common.MioUtil;
import com.mio.web.entity.message.Message;
import com.mio.web.entity.message.MessageSch;
import com.mio.web.entity.recommend.Recommand;
import com.mio.web.service.message.MessageService;

import net.coobird.thumbnailator.Thumbnails;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MessageController extends CrudController<Message, MessageService> {
	
	@RequestMapping("/message.do")
	public ModelAndView getMessageJsp(HttpServletRequest request){
		ModelAndView model = new ModelAndView("/message.jsp");
		//获取登录用户
		model.addObject("loginUser",UserContext.getInstance().getUser());
		//获取当前页数以及总页数
		String pages = request.getParameter("pageStart");
		String pageTotal = request.getParameter("pageCount");
		ExpressionQuery query = new ExpressionQuery();
		query.addSort("sortId");
		query.setPageSize(20);
		int pageNum  = 1;
		if(pages != null){
			pageNum = Integer.parseInt(pages);
			query.setStart((pageNum-1)*20);
		}else{
			query.setStart(0);
		}
		//获取当前页数的消息
		List<Message> messageLists = this.getService().find(query);
		model.addObject("messageLists",messageLists);
		List<Message> messages = this.getService().listIndexMessage();
		model.addObject("messages",messages);
		//设置起始页数
		model.addObject("pageStart", pageNum);
		int pageCount = 0;
		//判断是否有总页数的值
		if(pageTotal != null){
			int total = Integer.parseInt(pageTotal);
			pageCount = total;
		}else{
			//查询美美推荐的总页数
			query = new ExpressionQuery();
			pageCount = this.getService().findTotalCount(query)/20 + 1;
		}
		//设置总页数
		model.addObject("pageCount",pageCount);
		return model;
	}
	
	@RequestMapping("/messageDetail.do")
	public ModelAndView getMessageDetail(HttpServletRequest request){
		ModelAndView model = new ModelAndView("/messageDetail.jsp");
		//获取登录用户
		model.addObject("loginUser",UserContext.getInstance().getUser());
		String messageId = request.getParameter("messageId");
		Message message = this.getService().get(messageId);
		model.addObject("message",message);
		List<Message> messages = this.getService().listIndexMessage();
		model.addObject("messages",messages);
		return model;
	}

	// ������¼
	@RequestMapping("/addMessage.do")
	public @ResponseBody MessageResult addMessage(Message entity) {
		entity.setCreatedTime(MioUtil.getYMDHMSDateTime(new Date()));
		return this.save(entity);
	}

	// ɾ����¼
	@RequestMapping("/delMessage.do")
	public @ResponseBody MessageResult delMessage(Message entity) {
		return this.delete(entity);
	}

	// �޸ļ�¼
	@RequestMapping("/updateMessage.do")
	public @ResponseBody MessageResult updateMessage(Message entity) {
		return this.update(entity);
	}

	// ������ѯ��ҳ����
	@RequestMapping("/listMessage.do")
	public @ResponseBody GridResult listMessage(MessageSch searchEntitySch) {
		return this.query(searchEntitySch);
	}

	// ������ѯ���������м�¼
	@RequestMapping("/listAllMessage.do")
	public @ResponseBody Object listAllMessage(MessageSch searchEntitySch) {
		return this.queryAll(searchEntitySch);
	}

	// ��ȡ��ϸ��Ϣ
	@RequestMapping("/getMessageById.do")
	public @ResponseBody Message getMessageById(Message entity) {
		return this.getService().get(entity.getId());
	}

	/**
	 * 保存消息
	 * @param entity
	 * @param request
	 * @param file
	 * @return
	 */
	@RequestMapping("/saveMessage.do")
	public @ResponseBody Map<String,Object> saveMessage(Message entity,HttpServletRequest request,MultipartFile file) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			String path = request.getSession().getServletContext().getRealPath("upload/message");
			String relPath = "/upload/message/";
			String fileName = file.getOriginalFilename();
			// 大图
			File dir = MioUtil.createDir(path);
			File targetFile = MioUtil.createFile(dir, fileName);
			if (targetFile != null) {
				FileUtils.writeByteArrayToFile(targetFile, file.getBytes());
				// 同时保存小图到相应的文件中
				entity.setImgUrl(relPath+fileName);
				entity.setCreatedTime(MioUtil.getYMDHMSDateTime(new Date()));
				entity = this.getService().save(entity);			
				resultMap.put("success", true);
				resultMap.put("info", entity);
			} else {
				resultMap.put("success", false);
				resultMap.put("info", MioInfoTips.CREATE_FILE_FAIL);
			}

		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
			resultMap.put("info", e.getMessage());
		}
		return resultMap;
	}

}